package com.hghivln.erpwarehouse.business.controller;


import com.hghivln.erpwarehouse.business.entity.Provider;
import com.hghivln.erpwarehouse.business.service.ProviderService;
import com.hghivln.erpwarehouse.business.vo.ProviderVo;
import com.hghivln.erpwarehouse.system.common.lang.Result;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hghivln
 * @since 2021-07-10
 */
@RestController
@RequestMapping("api/provider")
public class ProviderController {

    @Autowired
    private ProviderService providerService;

    @RequestMapping("loadAllProvider")
    public Object loadAllProvider(ProviderVo providerVo) {
        return providerService.QueryAllProvider(providerVo);
    }

    @RequiresPermissions("provider:add")
    @RequestMapping("addProvider")
    public Result addProvider(Provider provider) {
        try {
            providerService.addProvider(provider);
            return Result.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ADD_ERROR;
        }
    }

    @RequestMapping("getAllAvailableProvider")
    public Object getAllAvailableProvider(){
        return providerService.getAllAvailableProvider();
    }

    @RequiresPermissions("provider:update")
    @RequestMapping("updateProvider")
    public Result updateProvider(Provider provider) {
        try {
            providerService.updateProvider(provider);
            return Result.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.UPDATE_ERROR;
        }
    }

    @RequiresPermissions("provider:delete")
    @RequestMapping("deleteProvider")
    public Result deleteProvider(Integer id) {
        try {
            providerService.removeById(id);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }

    @RequiresPermissions("provider:batchdelete")
    @RequestMapping("batchDeleteProvider")
    public Result batchDeleteProvider(Integer[] ids){
        try {
            Collection<Serializable> idList = new ArrayList<>(Arrays.asList(ids));
            providerService.removeByIds(idList);
            return Result.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return Result.DELETE_ERROR;
        }
    }
}
